package cn.sc.pai.test;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

/**
 * description：
 * author：pany
 * on 2018/9/20 14:18
 */
public class ViewDragLayout extends FrameLayout {

    private ViewDragHelper viewDrag;

    public ViewDragLayout(@NonNull Context context) {
        super(context);
        init();
    }

    public ViewDragLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ViewDragLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        viewDrag = ViewDragHelper.create(this, 1.0f, viewDragCallback);
        //滑动左边缘还是右边缘：EDGE_LEFT和EDGE_RIGHT检测
        viewDrag.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return viewDrag.shouldInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        viewDrag.processTouchEvent(event);
        return true;
    }

    private ViewDragHelper.Callback viewDragCallback = new ViewDragHelper.Callback() {

        //系统会依次列出这个父容器的子视图，你需要指定当前传入的这个视图是否可拖动
        @Override
        public boolean tryCaptureView(View child, int pointerId) {
            int id = child.getId();
            if (R.id.btn1 == id) {
                return true;
            } else if (R.id.btn2 == id) {
                return true;
            }
            return false;
        }

        //能滑动的最大（或者负向最大）的横向坐标
        @Override
        public int clampViewPositionHorizontal(View child, int left, int dx) {
            int rightLimit = getMeasuredWidth() - child.getMeasuredWidth();//右侧边界
            if (left > rightLimit) {
                return rightLimit;
            } else if (left < 0) {
                return 0;
            }
            return left;
        }

        //能滑动的最大（或者负向最大）的纵向坐标
        @Override
        public int clampViewPositionVertical(View child, int top, int dy) {
            int bottomLimit = getMeasuredHeight() - child.getMeasuredHeight();//底部边界
            if (top > bottomLimit) {
                return bottomLimit;
            } else if (top < 0) {
                return 0;
            }
            return top;
        }

        //当手指从子视图松开时，会调用这个函数，同时返回在x轴和y轴上当前的速度
        @Override
        public void onViewReleased(View releasedChild, float xvel, float yvel) {
            super.onViewReleased(releasedChild, xvel, yvel);
            Toast.makeText(getContext(),xvel+"=速度",Toast.LENGTH_SHORT).show();
        }

        //当边缘开始拖动的时候调用，根据前面设置，当左边缘滑动的时候回调
        @Override
        public void onEdgeDragStarted(int edgeFlags, int pointerId) {
            super.onEdgeDragStarted(edgeFlags, pointerId);
        }

        //返回这个索引所指向的子视图的Z轴坐标
        @Override
        public int getOrderedChildIndex(int index) {
            return super.getOrderedChildIndex(index);
        }

        //返回指定View在横向上能滑动的最大距离
        @Override
        public int getViewHorizontalDragRange(View child) {
            return super.getViewHorizontalDragRange(child);
        }

        //返回指定View在纵向上能滑动的最大距离
        @Override
        public int getViewVerticalDragRange(View child) {
            return super.getViewVerticalDragRange(child);
        }

        //返回指定的边是否被锁定
        @Override
        public boolean onEdgeLock(int edgeFlags) {
            return super.onEdgeLock(edgeFlags);
        }

        //当边缘被触摸时，系统会回调这个函数
        @Override
        public void onEdgeTouched(int edgeFlags, int pointerId) {
            super.onEdgeTouched(edgeFlags, pointerId);
        }

        //当有一个子视图被指定为可拖动时，系统会回调这个函数
        @Override
        public void onViewCaptured(View capturedChild, int activePointerId) {
            super.onViewCaptured(capturedChild, activePointerId);
        }

        //拖动状态改变时，会回调这个函数
        @Override
        public void onViewDragStateChanged(int state) {
            super.onViewDragStateChanged(state);
        }

        //当子视图位置变化时，会回调这个函数
        @Override
        public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
            super.onViewPositionChanged(changedView, left, top, dx, dy);
        }
    };
}
